import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex);

const store = new Vuex.Store({
    // data
    state:{
        userInfo: JSON.parse(window.localStorage.getItem('userInfo')),
        projects: JSON.parse(window.localStorage.getItem('projects'))?JSON.parse(window.localStorage.getItem('projects')):[],
        delProjects: JSON.parse(window.localStorage.getItem('delProjects'))?JSON.parse(window.localStorage.getItem('delProjects')):[],
    },
    // 计算属性
    getters:{

    },
    // methods
    mutations:{
        // 获取用户的登录信息
        setUserInfo: (state,value)=>{
            state.userInfo = value;
            window.localStorage.setItem('userInfo',JSON.stringify(value));
        },
        // 登出清除登录状态
        logout:(state)=>{
            state.userInfo = null;
            window.localStorage.removeItem('userInfo');
        },
        updateProjects:(state,value)=>{
            // 如果重新编辑
            if(value.code == 1){
                state.projects[value.id] = value.content;
            }else{
                state.projects.push(value.value)
            }
            window.localStorage.setItem('projects',JSON.stringify(state.projects))
        },
        // 删除项目更新项目列表
        delProject:(state,value)=>{
            state.projects.splice(value,1);
            window.localStorage.setItem('projects',JSON.stringify(state.projects));
        },
        // 刷新主页面重新获取项目列表
        fresh: (state)=>{
            if(window.localStorage.getItem('projects')){
                state.projects = JSON.parse(window.localStorage.getItem('projects'));
            }
        },
        // 删除的项目进入回收站
        toRefuse:(state,value) => {
            state.delProjects.push(value);
            window.localStorage.setItem('delProjects',JSON.stringify(state.delProjects));
        },
        // 恢复项目
        resume:(state,value)=>{
            state.delProjects.splice(value,1);
            window.localStorage.setItem('delProjects',JSON.stringify(state.delProjects));
        },
        // 彻底删除
        remove:(state,value)=>{
            store.commit('resume',value.index);
            window.localStorage.removeItem(value.name);
        }
    },
    // 异步方法
    actions:{

    },
    // 模块
    modules:{

    }
})

export default store;